import csv

from lxml import etree
import requests

def get_html(url):
    head={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"}
    try:
        r=requests.get(url=url,params=data,headers=head)
        r.encoding=r.apparent_encoding
        r.raise_for_status()
        return r.text
    except Exception as e:
        print(e)
def parser(html):
    html_tree = etree.HTML(html)
    out_list=[]
    for row in html_tree.xpath('//*[@id="book-img-text"]/ul/li/div[2]'):
        row_data=[row.xpath('h4/a/text()')[0],
                  row.xpath('p[1]/a[1]/text()')[0],
                  row.xpath('p[2]/text()')[0].strip(),
                  row.xpath('p[3]/span/text()')[0]]
        out_list.append(row_data)
    return out_list
def save_csv(path,content):
    with open(path,'a+',encoding='utf-8',newline='')as f:
        csv_write=csv.writer(f)
        csv_write.writerows(content)
if __name__=='__main__':
    for i in range(1,6):
        url='http://www.bspider.top/qidian/?'
        data={"page":i}
        yuanma=get_html(url)
        shuju=parser(yuanma)
        path='d:\\xiaoshuo.csv'
        save_csv(path,shuju)